<?php
/**
 * Created by PhpStorm.
 * User: VJLau
 * Date: 2018/2/5
 * Time: 下午5:55
 */

$op = empty($_GPC['op']) ? '' : $_GPC['op'];
$member = unserialize(base64_decode($_SESSION['vj_member']));
$uniacid = $_W['uniacid'];

if ($op == 'detail') {
    //用户信息页面
    $type = $_GPC['type'];
    $id = $_GPC['id'];
    if (empty($id)) message('参数错误', $this->createMobileUrl('me'), 'error');
    switch ($type) {
        case "single":
            $order = pdo_fetch("SELECT * FROM "
                . tablename('bontrip_single_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
            break;
        case "oneday":
            $order = pdo_fetch("SELECT * FROM "
                . tablename('bontrip_oneday_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
            break;
        case "quality":
            $order = pdo_fetch("SELECT * FROM "
                . tablename('bontrip_quality_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
            break;
        default:
            message('参数错误', $this->createMobileUrl('me'), 'error');
            break;
    }

    if (empty($order)) message('订单不存在', url('entry', array('m' => 'vj_bontrip', 'do' => 'order', 'op' => 'list', 'type' => $type), true), 'error');
    if (!empty($order['sku'])) {
        $order['sku'] = unserialize($order['sku']);
    }
    //出游人数
    $tour_str = '';
    if ($order['adult_number'] != 0) {
        $tour_str .= $order['adult_number'] . '成人';
        $order['adult_members'] = unserialize($order['adult_members']);
    }
    if ($order['child_number'] != 0) {
        $tour_str .= $order['child_number'] . '儿童';
        $order['child_members'] = unserialize($order['child_members']);
    }
    if ($order['baby_number'] != 0) {
        $tour_str .= $order['baby_number'] . '婴儿';
        $order['baby_members'] = unserialize($order['baby_members']);
    }
    $title = '订单详情';
    include $this->template('order_detail');
} else {
    //统计
    $type = $_GPC['type'];
    $pindex = max(1, intval($_GPC['page']));
    $psize = 4;
    switch ($type) {
        case "single":
            //单品
            $orders = pdo_fetchall("SELECT * FROM " . tablename('bontrip_single_order')
                . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null"
                . " ORDER BY id DESC LIMIT "
                . ($pindex - 1) * $psize . ',' . $psize);
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_single_order')
                . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null");

            $max_page = ceil($total / $psize);
            $is_next = true;

            if ($max_page <= $pindex) {
                $is_next = false;
            }
            $title = '单品';
            break;
        case "oneday":
            //一日游
            $orders = pdo_fetchall("SELECT * FROM " . tablename('bontrip_oneday_order')
                . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null"
                . " ORDER BY id DESC LIMIT "
                . ($pindex - 1) * $psize . ',' . $psize);
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_oneday_order')
                . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null");


            $max_page = ceil($total / $psize);
            $is_next = true;

            if ($max_page <= $pindex) {
                $is_next = false;
            }
            $title = '一日游';
            break;
        case "quality":
            //精品游
            $orders = pdo_fetchall("SELECT * FROM " . tablename('bontrip_oneday_order')
                . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null"
                . " ORDER BY id DESC LIMIT "
                . ($pindex - 1) * $psize . ',' . $psize);
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_oneday_order')
                . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null");

            $max_page = ceil($total / $psize);
            $is_next = true;

            if ($max_page <= $pindex) {
                $is_next = false;
            }
            $title = '精品游';
            break;
        case "insurance":
            //保险
            $orders = pdo_fetchall("SELECT * FROM " . tablename('bontrip_insurance_order')
                . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null"
                . " ORDER BY id DESC LIMIT "
                . ($pindex - 1) * $psize . ',' . $psize);
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_insurance_order')
                . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null");

            $max_page = ceil($total / $psize);
            $is_next = true;

            if ($max_page <= $pindex) {
                $is_next = false;
            }
            $title = '保险';
            break;
        default:
            message('参数错误', $this->createMobileUrl('me'), 'error');
            break;
    }

    include $this->template('order_list');
}

